﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BingoGameServerConsole
{
    /// <summary>
    /// 房间机器人逻辑业务
    /// </summary>
    public partial class Room
    {
        private List<User> robotList = new List<User>();//机器人列表
        private int controlRobotNum = 0;//每回合 控制机器人的 参数（真人+机器人数量）

        private long robot_wait_time = 0;
        private long robot_play_time = 0;

        private long ticks_02_time = 0;
        private long ticks_05_time = 0;


        private bool endTenBall = false;//是否结束头十球
        private int endSeconds = 0;

        private void StartRobot()
        {
            ClearRobot();
            endTenBall = false;
            controlRobotNum = rd.Next(44,56);
            endSeconds = 0;
            ticks_02_time = 0;
            ticks_05_time = 0;
            robot_play_time =robot_wait_time = DateTime.Now.Ticks;

            Console.WriteLine($"房间 {id} 开始机器人 ,机器人停止控制参数：{controlRobotNum}");
        }
        
        private void ClearRobot()
        {
            robotList.Clear();
        }
        
        private void UpdateRobot(long ticks)
        {
            #region 机器人生成逻辑
            if (selectCardRealPlayNum + robotList.Count< controlRobotNum)
            {//如果游戏玩家+机器人玩家数量 < 设定的数值  才产生机器人
                if (state == PlayState.PS_WAIT)
                {//等待状态 每隔1秒  产生机器人
                    if (ticks - robot_wait_time >= 10000000 * 1)
                    {

                        robot_wait_time = ticks;
                        int tmp = rd.Next(1, 101);
                        if (tmp <= 50)
                        {//50%的概率 跟进机器人
                            int num = rd.Next(1, 4);
                            for (int i = 0; i < num; i++)
                            {
                                User user = CreateRobot();
                                int robot_card = GetRobotCard();
                                UserSelectCard(user, robot_card, 1);
                            }
                        }

                    }
                }
                else if (state == PlayState.PS_PLAY)
                {//游戏状态 

                    if (ticks - startGameTime <= 10000000 * headTenTime)
                    {//时间在头十球时间内 每隔2秒 产生机器人
                        if (ticks - robot_play_time >= 10000000 * 1)
                        {

                            robot_play_time = ticks;

                            int tmp = rd.Next(1, 101);
                            if (tmp <= 30)
                            {//30%的概率 跟进机器人

                                User user = CreateRobot();
                                int robot_card = GetRobotCard();
                                UserSelectCard(user, robot_card, 1);

                            }

                        }
                    }
                }
            }
            #endregion

            #region 机器人bingo逻辑
            if(state == PlayState.PS_PLAY && ticks -startGameTime>10000000* headTenTime)
            {
                if(!endTenBall)
                {//刚好结束头十球 开始下一个阶段
                    endSeconds = rd.Next(27,46);
                    Console.WriteLine($"房间{id} 结束头十球，机器人随机结束时间为 {endSeconds} 后");

                }
                endTenBall = true;

                if(ticks-startGameTime- 10000000 * headTenTime <= 10000000* endSeconds)
                {//在结束时间范围内  每0.2S 检测bingo

                    if( ticks-ticks_02_time>= 10000*500&& bingoNum > 3)
                    {
                        ticks_02_time = ticks;
                        if(CheckBingo())
                        {
                            RobotBingo();
                        }
                    }
                }
                else if(bingoNum>0)
                {//每0.5s检测bingo
                    if (ticks - ticks_05_time >= 10000 * 1500)
                    {
                        ticks_05_time = ticks;
                        if (CheckBingo())
                        {
                            RobotBingo();
                        }
                    }
                }
            }
            #endregion


        }
        private bool CheckBingo()
        {
            long cur_time = (DateTime.Now.Ticks - startGameTime - 10000000 * headTenTime)/ 10000000;
            if (cur_time == 0) return false;
            float tmp_f= cur_time / (float)endSeconds;
            int tmp_i = rd.Next(1,101);
            //Console.WriteLine($"进度：{cur_time}/{endSeconds}");
            if(tmp_i <= tmp_f*100)
            {
                return true;
            }
            return false;
        }
        private void RobotBingo()
        {
            ///随机一个机器人进行bingo
            int index = rd.Next(0,robotList.Count);
            User user = robotList[index];
            Bingo(user);

            int num = bingoCountDict[user.id];
            if(num>=user.selectCardNum)//如果bingo数量 达到选卡数量 则 异常这个机器人
                robotList.RemoveAt(index);
        }
        private User CreateRobot()
        {
            User user = new User(Util.GetUserID(), (IntPtr)0);
            user.bRobot = true;
            user.name = "guest_" + user.id;
            user.icon = "";
            robotList.Add(user);
            Console.WriteLine($"房间 {id} 生成了1个机器人，当前机器人总数：{robotList.Count}");
            return user;
        }
        private int GetRobotCard()
        {
            int num = rd.Next(1,101);
            if(num<=10)
            {
                Console.WriteLine("机器人选卡 1 ");
                return 1;//10%概率选1卡
            }
            else if(num<=30)
            {
                Console.WriteLine("机器人选卡 2 ");
                return 2;//20%概率选2卡
            }
            else if( num<=90)
            {
                Console.WriteLine("机器人选卡 4 ");
                return 4;//60%概率选4卡
            }
            else if(num<=100)
            {
                Console.WriteLine("机器人选卡 8 ");
                return 8;//10%概率选8卡
            }
            
            return 0;
        }
    }
}
